package com.wendal.java.vfilesystem;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class VFileDataZone {

	private RandomAccessFile accessFile;

	private int blockSize;

	public VFileDataZone(File file, int blockSize) throws FileNotFoundException {
		this.blockSize = blockSize;
		accessFile = new RandomAccessFile(file, "rws");
	}

	public byte[] readBlock(int blockNumber) throws IOException {
		byte[] data = new byte[blockSize];
		accessFile.seek(blockNumber * blockSize);
		accessFile.read(data);
		return data;
	}

	public boolean writeBlock(int blockNumber, byte[] data) throws IOException {
		if (data == null)
			return false;
		if (data.length > blockSize)
			return false;
		accessFile.seek(blockNumber * blockSize);
		accessFile.write(data);
		return true;
	}

	public void reset() throws IOException {
		accessFile.setLength(0);
	}
}
